$.widget("tempo.sqlquerygroupmenu",{
    options:{
        //初始化是否显示
        url:'',
        data:[],
        param:{},
        onAfterExpand:$.noop,
        onCheckGroup:$.noop,
        //子项是否带有复选框
        check : false,
        //父项的唯一标识字段
        groupIdField : 'name',
        //子项是否带有操作按钮
        itemIcon : true,
        onCheckItem:$.noop,
        // 是否支持ctrl、shift选中
        onCtrlShiftSelect:$.noop,
        onClickItem:$.noop,
        onDblClickItem:$.noop,
        multSelect:false,
        leftPadding:8,
        isButtonEnable:false,
        isGroupButtonEnable: true,
        itemMenu:false,
        tooltip:$.noop,
        //自定义提示内容
        tooltipcontent:$.noop,
        //每一项进行格式化显示
        itemFormatter:function(item,data){
            return item;
        },
        canDrag:true,
        dragHandle:{
            //设置为true，只有在鼠标放在节点时候才会显示
            hover:true
        },
        resizeOptions:{
            minHeight:100,
            minWidth:120
        },
        dragOptions:{
            //可拖拽父容器
            helper:function(){
                var that  = $("<span class='tempo-groupmenu-drag-text'/>").text($(this).text());
                that.appendTo($("body")).css({
                    'display':'block',
                    'z-Index':1000,
                    'text-align':'center',
                    'border':'1px solid #ddd',
                    'cursor':'pointer',
                    'width':'171px',
                    'height':'30px',
                    'line-height':'30px',
                    'background':'#d7ecf8',
                    'overflow': 'hidden',
                    'white-space': 'nowrap',
                    'text-overflow': 'ellipsis'
                });
                return that;
            }
        },
        // 是否加载数据完成后绑定事件，设置为true可优化加载速度
        bindClickAfterAddData:false,
        markIcon:'tempo-ui-icon-setting',
        bTwoLevelCatalogues:false,
        dbms_tag:false,//添加参数：渲染拖拽table 默认false
        dragDisHeader:false //是否禁用根节点的拖拽 true为禁用 默认为false
    },
    _create:function(){
        //处理ie 选中文字问题
        if(typeof this.element[0].onselectstart!='undefined'){
            this.element[0].onselectstart = function(){return false};
        }
    },
    _init:function(){
        if(!this.element.is("div")){
            $.error("必须为div元素");
            return;
        }
        this.element
            .addClass("tempo-groupmenu");
        var data = this.options.data;
        this._decodeData();
    },
    _decodeData:function(data,parent,index){
        var $this = this;
        if(data && data.length > 0){
            index ++;
            for(var i=0;i<data.length;i++){
                var dataItem = data[i];
                var groupItem = $this._addItem(dataItem,parent,index);
                if(dataItem.children && dataItem.children.length>0){
                    $this._decodeData(dataItem.children,groupItem,index);
                }
            }
        }else{
            var groupData = $this.options.data;
            for(var i=0;i<groupData.length;i++){
                var children = groupData[i].children;
                var parentGroup = $this._addGroup(groupData[i]);
                if(children && children.length>0){
                    $this._decodeData(children,parentGroup,1);
                }
            }
            if ($this.options.bindClickAfterAddData){
                $this._bindAllClick($this);
            }
        }
    },
    addMenu: function(parent, index, data) {
        /**
         * 添加子节点列表，适用于异步加载
         */
        if(parent.hasClass('tempo-groupmenu-content-group')
            && parent.children().length == 0
            && $.isArray(data)) {
            this._decodeData(data, parent, index);
        }
    },
    addMenuItem: function(parent, index, data, addToPrev) {
        /**
         * 添加单个子节点，适用于树的增、删
         */
        if((parent.hasClass('tempo-groupmenu-content-group')
            || parent.hasClass('tempo-groupmenu-content'))
            && !$.isEmptyObject(data)) {
            this._addItem(data, parent, index, addToPrev);
        }
    },
    renameMenuItem:function(data, oldTitle){
        // this.removeMenuItem(oldTitle);
        // this.addMenuItem(this.element.find("div[formenuid=TEMPO_SQL_QUERY_COLCALCULATE]"),2,data);
        var id = oldTitle;
        var name = data.title;
        var node = this._getNodeById(id);
        node.find('.tempo-groupmenu-item-text').text(name);
        node.attr("menuid", name);
        debugger;
        node.data('data',data);
    },
    removeMenuItem:function(id) {
        var node = this._getNodeById(id);
        node.next().remove();
        node.remove();
    },
    getNodeById:function(id){
        var $thisElement = this.element;
        var item = $thisElement.find("div[menuid='"+id+"']");
        return item;
    },
    _bindAllClick:function(){
        var $this = this;
        var $thiselement = $this.element;
        var opts = $this.options;
        var groupTitle = $thiselement.find(".tempo-groupmenu-title");
        groupTitle.unbind('click').bind('click',function(){
            var effect = 10;
            if(!$(this).hasClass("tempo-groupmenu-menu-select")){
                $thiselement.find('.tempo-groupmenu-item-select').removeClass('tempo-groupmenu-item-select');
                if($(this).hasClass("tempo-groupmenu-menu-hasChildren")){
                    if($(this).hasClass("tempo-groupmenu-menu-select")){
                        $(this).removeClass("tempo-groupmenu-menu-select");
                        $(this).next().hide(10);
                    }else{
                        if(opts.multSelect === false){//不支持多选
                            $("[menuid]",$thiselement).removeClass("tempo-groupmenu-menu-select");
                            $("[formenuid]",$thiselement).hide(10);
                        }
                        $(this).addClass("tempo-groupmenu-menu-select");
                        $(this).next().show(10);
                    }
                }else{
                    if(opts.multSelect === false){//不支持多选
                        $("[menuid]",$thiselement).removeClass("tempo-groupmenu-menu-select");
                        $("[formenuid]",$thiselement).hide(10);
                    }
                }
                var data = $(this).data('data');
                opts.onAfterExpand.call(this,data);
            }else{
                $(this).next().hide(10);
                var display = $(this).next().css('display');
                if(display == "none"){
                    $(this).next().show(10);
                }else{
                    $(this).next().hide(10);
                }
            }

        });

        if(opts.isButtonEnable){
            groupTitle.unbind('mouseover').bind('mouseover',function(){
                $(this).find('.tempo-groupmenu-title-mark').show();
            }).unbind('mouseout').bind('mouseout',function(){
                $(this).find('.tempo-groupmenu-title-mark').hide();
            });
        }
        if(opts.onCheckGroup){
            groupTitle.find('.tempo-groupmenu-title-mark').unbind('click').bind('click',function(e){
                var currentData = $(this).data('data');
                opts.onCheckGroup(this,e,currentData);
            });
        }
    },
    _addGroup:function(currentNodeData){
        var $this = this;
        var $thiselement = $this.element;
        var opts = $this.options;

        var data = TempoUtils.clone(currentNodeData);
        var id = data[opts.groupIdField];
        var text = data.title || "";


        var tooltipMsg = text
        if(opts.tooltip != $.noop){
            tooltipMsg = opts.tooltip.call(this,data);
        }

        //如果有自定义提示内容，则不需要增加title属性
        if(opts.tooltipcontent != $.noop){
            tooltipMsg = '';
        }

        if(opts.itemFormatter){
            text = opts.itemFormatter.call(this.element,text,data);
        }
        var groupTitle = $("<div class='tempo-groupmenu-title' menuid='"+id+"' />");
        var groupContent = $("<div class='tempo-groupmenu-content' formenuid='"+id+"' style='display:none;'/>");
        var imgClass = data.icon;
        if(data.type === "COLCALCULATE") {
            groupTitle.html("<div class='tempo-groupmenu-title-content'>" +
                "<span class='tempo-groupmenu-title-icon "+imgClass+"'/>"+
                "<span class='tempo-groupmenu-title-text' title='" + tooltipMsg + "'>"+text +"</span>"+
                "</div>");
        } else {
            groupTitle.html("<div class='tempo-groupmenu-title-content'>" +
                "<span class='tempo-groupmenu-title-icon "+imgClass+"'/>"+
                "<span class='tempo-groupmenu-title-text' title='" + tooltipMsg + "'>"+text +"</span>"+
                "<span class='tempo-groupmenu-title-mark ui-icon tempo-ui-icon tempo-ui-icon-no-mutual "+ opts.markIcon +"'/>"+
                "</div>");
        }

        //缓存数据
        groupTitle.data("data",data);
        $thiselement.append(groupTitle);
        $thiselement.append(groupContent);
        if (!$this.options.bindClickAfterAddData){
            groupTitle.unbind('click').bind('click',function(){
                var effect = 10;
                if(!$(this).hasClass("tempo-groupmenu-menu-select")){
                    $thiselement.find('.tempo-groupmenu-item-select').removeClass('tempo-groupmenu-item-select');
                    if($(this).hasClass("tempo-groupmenu-menu-hasChildren")){
                        if($(this).hasClass("tempo-groupmenu-menu-select")){
                            $(this).removeClass("tempo-groupmenu-menu-select");
                            $(this).next().hide(10);
                        }else{
                            if(opts.multSelect === false){//不支持多选
                                $("[menuid]",$thiselement).removeClass("tempo-groupmenu-menu-select");
                                $("[formenuid]",$thiselement).hide(10);
                            }
                            $(this).addClass("tempo-groupmenu-menu-select");
                            $(this).next().show(10);
                        }
                    }else{
                        if(opts.multSelect === false){//不支持多选
                            $("[menuid]",$thiselement).removeClass("tempo-groupmenu-menu-select");
                            $("[formenuid]",$thiselement).hide(10);
                        }
                    }
                    var data = $(this).data('data');
                    opts.onAfterExpand.call(this,data);
                }else{
                    $(this).next().hide(10);
                    var display = $(this).next().css('display');
                    if(display == "none"){
                        $(this).next().show(10);
                    }else{
                        $(this).next().hide(10);
                    }
                }

            });

            if(opts.isButtonEnable && opts.isGroupButtonEnable){
                groupTitle.unbind('mouseover').bind('mouseover',function(){
                    $(this).find('.tempo-groupmenu-title-mark').show();
                }).unbind('mouseout').bind('mouseout',function(){
                    $(this).find('.tempo-groupmenu-title-mark').hide();
                });
            }
            if(opts.onCheckGroup){
                groupTitle.find('.tempo-groupmenu-title-mark').unbind('click').bind('click',function(e){
                    var currentData = groupTitle.data('data');
                    opts.onCheckGroup(this,e,currentData);
                });
            }

            //自定义属性
            if(opts.tooltipcontent != $.noop){
                groupTitle.find("span.tempo-groupmenu-title-text").tooltip({
                    items:'.tempo-groupmenu-title-text',
                    track: true,
                    content:function(){
                        try{
                            var tooltipcontent = opts.tooltipcontent||$.noop;
                            var content = tooltipcontent.call($this,currentNodeData);
                            return content;
                        }catch (e) {}
                    }
                });
            }
        }
        groupContent.data("for-item",groupTitle);
        this._dragItem(groupContent, groupTitle, data);
        return groupContent;
    },

    _addItem :function(currentNodeData,pct,index,addToPrev){
        var data = TempoUtils.clone(currentNodeData);
        var id = currentNodeData.name;
        var text = currentNodeData.title||"";

        var $this = this;
        var $thisElement = this.element;
        var opts = this.options;
        var groupType = data.groupType;
        var type = data.type;

        var tooltipMsg = text;
        if(opts.tooltip != $.noop){
            tooltipMsg = opts.tooltip.call(this,data);
        }

        //如果有自定义提示内容，则不需要增加title属性
        if(opts.tooltipcontent != $.noop){
            tooltipMsg = '';
        }

        if(opts.itemFormatter){
            text = opts.itemFormatter.call(this.element,text,data);
        }

        //是否禁用拖拽
        var disableDrag = data.disableDrag;
        //是否禁用菜单功能
        var disableMenu = data.disableMenu;

        var imgClass = data.icon;

        var groupItemContent = $("<div class='tempo-groupmenu-content-group'/>").attr('index', index);
        
        var item = $("<div class='tempo-groupmenu-item' groupid='" + groupType + "' menuid='" + ((data.alias||data.id)||id) + "' />");

        var html = "<div class='tempo-groupmenu-item-content' style='left:"+((index-1)*opts.leftPadding)+"px;'>";
        if(opts.check){
            html += "<input type='checkbox' class='tempo-groupmenu-item-check'>";
        }
        if(opts.itemIcon){
            html += "<span class='tempo-groupmenu-item-icon "+imgClass+"' />";
        }
        var className = "tempo-groupmenu-item-text";
        if(disableDrag) {
            //如果禁用拖拽，使用no drag class
            className += " tempo-groupmenu-item-no-drag";
        }
        html +=	"<span class='"+className+"' title='"+tooltipMsg+"' style='width: 100px !important;'>"+text+
            "</span>"
        // if(opts.itemMenu && !disableMenu){
        //     //如果全局显示菜单并且当前节点不禁用菜单，增加菜单标签
        //     html += "<span class='tempo-groupmenu-item-mark ui-icon tempo-ui-icon tempo-ui-icon-no-mutual "+ opts.markIcon +"' /></span>";
        // }
        if(currentNodeData.isCalculationColumn || (currentNodeData.gather && currentNodeData.gather != "")) {//如果是计算列
            html += "<span class='tempo-groupmenu-item-mark ui-icon tempo-ui-icon tempo-ui-icon-no-mutual tempo-ui-icon-delete' style='margin-right: 15px !important;'/>";
        }
        html +=	"</div>";
        item.html(html);

        //item.find(".tempo-groupmenu-item-text").attr('title',text);
        if(opts.check){
            item.find(".tempo-groupmenu-item-check").data('data', data);
        }
        var cacheDataInfo = TempoUtils.clone(data);
        //缓存数据
        delete cacheDataInfo.children;
        item.data("data",data);
        if (addToPrev){
            // 添加元素到最前面
            pct.prepend(groupItemContent);
            if (!opts.bTwoLevelCatalogues || (tooltipMsg && tooltipMsg.length > 0)) {
                pct.prepend(item);
            }
        } else {
            if (!opts.bTwoLevelCatalogues || (tooltipMsg && tooltipMsg.length > 0)) {
                pct.append(item);
            }
            pct.append(groupItemContent);
        }
        pct.data("for-item").addClass("tempo-groupmenu-menu-hasChildren");


        if(type != "group"){
            
            if(opts.isButtonEnable && (currentNodeData.isCalculationColumn || currentNodeData.gather != "")){
                item.unbind('mouseover').bind('mouseover',function(e){
                    
                    $(this).find('.tempo-groupmenu-item-mark').show();
                }).unbind('mouseout').bind('mouseout',function(e){
                    
                    $(this).find('.tempo-groupmenu-item-mark').hide();
                });
            }

            if(opts.onCheckItem != $.noop){
                item.find('.tempo-groupmenu-item-mark').unbind('click').bind('click',function(e){
                    var data = item.data('data');
                    opts.onCheckItem.call(this,e,data);
                });
            }

            if(opts.onClickItem != $.noop){
                item.find('.tempo-groupmenu-item-text').unbind('click').bind('click',function(e){
                    var currentData = item.data('data');
                    $this.selectItem(currentData.nodeType || currentData.name);
                    opts.onClickItem.call(this,currentData,e);
                });
                item.find('.tempo-groupmenu-item-icon').unbind('click').bind('click',function(e){
                    var currentData = item.data('data');
                    $this.selectItem(currentData.nodeType || currentData.name);
                    opts.onClickItem.call(this,currentData,e);
                });
            }
            if(opts.onCtrlShiftSelect != $.noop){
                item.unbind('click').bind('click',function(e){
                    var currentData = item.data('data');
                    opts.onCtrlShiftSelect.call(this,currentData,e);
                });
            }
            if(opts.onDblClickItem != $.noop){
                item.find('.tempo-groupmenu-item-text').unbind('dblclick').bind('dblclick',function(){
                    var currentData = item.data('data');
                    $this.selectItem(currentData.nodeType || currentData.name);
                    opts.onDblClickItem.call(this,currentData);
                });
            }

            if(opts.tooltipcontent != $.noop){
                
                item.find("span.tempo-groupmenu-item-text").tooltip({
                    items:'.tempo-groupmenu-item-text',
                    track: true,
                    content:function(){
                        var $child = $(this);
                        try{
                            var tooltipcontent = opts.tooltipcontent||$.noop;
                            var content = tooltipcontent.call($this,currentNodeData,$child);
                            return content;
                        }catch (e) {}
                    }
                });
            }
        }else{
            // 如果设置默认关闭，则选择的节点不展开
            if(!opts.isDefaultClosed) {
                item.addClass("tempo-groupmenu-item-group-select");
            } else {
                groupItemContent.css('display', 'none');
            }
            item.unbind('click').bind('click',function(){
                // ui-icon tempo-ui-icon tempo-ui-icon-downarrows-undertint

                if($(this).hasClass("tempo-groupmenu-item-group-select")){
                    $(this).next().hide();
                    $(this).removeClass("tempo-groupmenu-item-group-select");
                }else{
                    $(this).next().show();
                    $(this).addClass("tempo-groupmenu-item-group-select");
                }
                var groupItemIcon = item.find(".tempo-groupmenu-item-icon");
                if(groupItemIcon.hasClass("ui-icon tempo-ui-icon tempo-ui-icon-downarrows-undertint")){
                    groupItemIcon.removeClass("ui-icon tempo-ui-icon tempo-ui-icon-downarrows-undertint");
                    groupItemIcon.addClass("ui-icon tempo-ui-icon tempo-ui-icon-rightarrows-undertint");
                }else if(groupItemIcon.hasClass("ui-icon tempo-ui-icon tempo-ui-icon-rightarrows-undertint")){
                    groupItemIcon.removeClass("ui-icon tempo-ui-icon tempo-ui-icon-rightarrows-undertint");
                    groupItemIcon.addClass("ui-icon tempo-ui-icon tempo-ui-icon-downarrows-undertint");
                }
            });
        }
        groupItemContent.data("for-item",item);
        this._dragItem(pct, pct.prev(), data);
        return groupItemContent;
    },
    /**
     * groupContent是.tempo-groupmenu-content
     * groupTitle是.tempo-groupmenu-title
     */
    _dragItem:function(groupContent,groupTitle, nodeData){
        if (!this.options.canDrag || (nodeData && nodeData.disableDrag)){
            // 禁用拖拽
            return;
        }
        //处理事件作用域问题
        var accordion = this;
        var opts = $.extend(
            this.options.dragOptions,{
                start:function(evt,ui){
                    return accordion._dragStart.call(accordion,evt,ui);
                },
                drag:function(evt,ui){
                    accordion._draging.call(accordion,evt,ui);
                },
                stop:function(evt,ui){
                    accordion._dragStop.call(accordion,evt,ui);
                }
            });
        //ifelse
        if(this.options.dbms_tag == true){
            //给最外层添加拖拽事件
            var tableNode = groupTitle.parent().find(".tempo-ui-icon-dataset-table,.tempo-ui-icon-dataset-sql");
            var tableAddDrag = tableNode.next('.tempo-groupmenu-item-text,.tempo-groupmenu-title-text');
            tableAddDrag.draggable(opts);
            tableNode.parent().parent('.tempo-groupmenu-title').next('.tempo-groupmenu-content').find('.tempo-groupmenu-item-text').draggable(opts);
            tableNode.parent().parent('.tempo-groupmenu-item').next('.tempo-groupmenu-content').find('.tempo-groupmenu-item-text').draggable(opts);
        }else{
            //加事件只能加到子节点上，不能加到本节点上
            groupContent.find('.tempo-groupmenu-item-text:not(.tempo-groupmenu-item-no-drag)').draggable(opts);
            if(!this.options.dragDisHeader){
                groupTitle.find('.tempo-groupmenu-title-content').draggable(opts);
            }
        }
    },
    _dragStart:function(evt,ui){
        if(this.options.dragOptions.dragStart){
            var result = this.options.dragOptions.dragStart.call(this,evt,ui);
            if(typeof result == 'boolean'){
                return result;
            }
        }
        return true;
    },
    _draging:function(evt,ui){
        if(this.options.dragOptions.draging){
            this.options.dragOptions.draging.call(this,evt,ui);
        }
    },
    _dragStop:function(evt,ui){
        this._setStyle();
        if(this.options.dragOptions.dragStop){
            this.options.dragOptions.dragStop.call(this,evt,ui);
        }
    },
    _setStyle:function(){
        var position = this.element.position();
        var width = this.element.width();
        var height = this.element.height();
        this.options.data['style'] = {
            left:position.left,
            top:position.top,
            zindex:1000,
            width:width,
            height:height,
            position:'absolute'
        };
    },
    _destroy:function(){
        if(this.options.resizable){
            this.element.resizable("destroy");
        }
        this.element.removeData();
        this.element.children().remove();
        this.element.removeClass('tempo-groupmenu');
    },
    cacheData:function(obj,data){
        delete data.children;
        var name = data.name;
        data.name = data.alias || name;
        obj.data("data",data);
    },
    getGroupIcon:function(type){
        var typeIcon = {
            "table":'table',
            "sql":'sql',
            "file":'file',
            "collect":"collect",
            "chart":'chart'
        };
        var uri = typeIcon[type]?typeIcon[type]:'default';
        return uri;
    },
    //处理子节点图片
    groupItemIcon:function(type){
        var typeIcon = {
            "STRING":'string',
            "NUMBER":'number',
            "DATE":'date',
            "POSITION":'position'
        };
        var uri = typeIcon[type]?typeIcon[type]:'default';
        return uri;
    },
    _getNodeById:function(id){
        var $thisElement = this.element;
        var item = $thisElement.find("div[menuid='"+id+"']");
        return item;
    },
    /**
     * 展开节点
     */
    expand:function(id){
        var menu = this._getNodeById(id);
        menu.addClass('tempo-groupmenu-menu-select').next().show();
    },
    /**
     * 主节点重命名
     */
    groupRename:function(id,name){
        var $this = this;
        var opts = $this.options;
        var menu = $this._getNodeById(id);
        menu.data('data').title = name;
        menu.find('.tempo-groupmenu-title-text').text(name);
        menu.find('.tempo-groupmenu-title-text').attr("title",name);
        menu.find('.tempo-groupmenu-title-text').tooltip({
            items:'.tempo-groupmenu-title-text',
            track: true,
            content:function(){
                try{
                    var tooltipcontent = opts.tooltipcontent||$.noop;
                    var content = tooltipcontent.call($this,menu.data('data'));
                    return content;
                }catch (e) {}
            }
        });
    },
    /**
     * 删除主节点
     */
    removeGroup:function(id){
        var $thisElement = this.element;
        var item = $thisElement.find('div[menuid]').filter(function(index) {
            if($(this).attr('menuid') == id){
                $(this).next().remove();
                $(this).remove();
            }
        });
    },
    addGroup:function(data){
        this._addGroup(data);
    },
    /**
     * 添加子节点
     */
    addItem:function(data,pid){
        var ruleconstant = {
            datatype:{
                STRING:"STRING",
                INT:"INT",
                DOUBLE:"DOUBLE",
                DATE:"DATE",
                POSITION:"POSITION"
            }
        };
        var $this = this;
        var $thisElement = this.element;
        var opts = this.options;
        if(data && data.length > 0){
            var groupContent = $("<div class='tempo-groupmenu-content' formenuid='"+pid+"' />");
            for(var i=0;i<data.length;i++){
                var dataItem = data[i];
                var imgClass = dataItem.icon;
                var tooltipMsg = dataItem.title;
                if(opts.tooltip != $.noop){
                    tooltipMsg = opts.tooltip.call(this,dataItem);
                }
                var item = $("<div class='tempo-groupmenu-item' menuid='"+dataItem.name+"' />");
                item.html("<div class='tempo-groupmenu-item-content' title='"+tooltipMsg+"'>" +
                    "<span class='tempo-groupmenu-item-icon "+imgClass+"' />"+
                    "<span class='tempo-groupmenu-item-text'  style='width: 100px !important;'></span>"+
                    "<span class='tempo-groupmenu-item-mark ui-icon tempo-ui-icon tempo-ui-icon-no-mutual "+ opts.markIcon +"' style='margin-right: 15px !important;'/>"+
                    "</div>");
                item.appendTo(groupContent);
                item.data('data',dataItem);
                item.find("span.tempo-groupmenu-item-text").text(dataItem.title);
                item.find('.tempo-groupmenu-item-text').tooltip({
                    items:'.tempo-groupmenu-item-text',
                    track: true,
                    content:function(){
                        try{
                            var tooltipcontent = opts.tooltipcontent||$.noop;
                            var content = tooltipcontent.call($this);
                            return content;
                        }catch (e) {}
                    }
                });
            }
            groupContent.find('.tempo-groupmenu-item-content').css("left",opts.leftPadding+"px");
            var parent = this._getNodeById(pid);
            parent.addClass("tempo-groupmenu-menu-hasChildren");
            parent.addClass("tempo-groupmenu-menu-select");
            parent.after(groupContent);
            $this._dragItem(groupContent,parent,data);
        }
    },
    /**
     * 选中Item
     */
    selectItem:function(id){
        var $thisElement = this.element;
        $thisElement.find(".tempo-groupmenu-item-select").removeClass("tempo-groupmenu-item-select");
        var item = $thisElement.find('div[menuid]').filter(function(index) {
            return $(this).attr('menuid') == id;
        });
        item.addClass("tempo-groupmenu-item-select");
        return item;
    },
    /**
     * 选中标题
     */
    toHighLight:function(id){
        var $thisElement = this.element;
        var item = $thisElement.find('div[menuid]').filter(function(index) {
            if($(this).attr('menuid') == id){
                $(this).addClass("tempo-groupmenu-title-selected");
            }
        });
    },
    clearHighLight:function(){
        var $thisElement = this.element;
        $thisElement.find(".tempo-groupmenu-title-selected").removeClass("tempo-groupmenu-title-selected");
    },
    /**
     * 获取各主节点的数据
     */
    getGroupData:function(){
        var $thisElement = this.element;
        var groups = $thisElement.find('.tempo-groupmenu-title');
        var groupData = {};
        for(var i=0;i<groups.length;i++){
            var id = $(groups[i]).attr("menuid");
            var data = $(groups[i]).data("data");
            groupData[id] = data;
        }
        return groupData;
    },
    /**
     * 获取选中的group
     */
    getGroupSelected:function(){
        var $thisElement = this.element;
        var groups = $thisElement.find('.tempo-groupmenu-menu-select');
        var data = groups.data("data");
        if(!data)
            return null;
        return data;
    },

    /**
     * 获取选中的item包括group和item
     */
    getItemSelected:function(){
        var $thisElement = this.element;
        var groups = $thisElement.find('.tempo-groupmenu-item-select');
        var data = groups.data("data");
        if(!data)
            return null;
        return data;
    },
    /**
     * 获取当前组件的原始数据
     */
    getData:function(){
        return this.options.data;
    },
    destroy:function(){
        this._destroy();
    },
    /**
     * 拖动指定元素
     */
    dragEle:function(thisEle){
        //处理事件作用域问题
        var accordion = this;
        var opts = $.extend(
            this.options.dragOptions,{
                start:function(evt,ui){
                    accordion._dragStart.call(accordion,evt,ui);
                },
                drag:function(evt,ui){
                    accordion._draging.call(accordion,evt,ui);
                },
                stop:function(evt,ui){
                    accordion._dragStop.call(accordion,evt,ui);
                }
            });
        thisEle.draggable(opts);
    }
});